//乘积最⼤⼦数组（medium）: https://leetcode.cn/problems/maximum-product-subarray/
class Solution {
public:
    int maxProduct(vector<int>& nums) {
        // 1. 创建 dp 表
        // 2. 初始化
        // 3. 填表
        // 4. 返回结果

        int n = nums.size();
        vector<int> f(n + 1), g(n + 1);
        f[0] = g[0] = 1;
        int ret = INT_MIN;
        for (int i = 1; i <= n; i++) {
            int x = nums[i - 1], y = f[i - 1] * nums[i - 1],
                z = g[i - 1] * nums[i - 1];
            f[i] = max(x, max(y, z));
            g[i] = min(x, min(y, z));
            ret = max(ret, f[i]);
        }
        return ret;
    }
};

//乘积为正数的最⻓⼦数组（medium）: https://leetcode.cn/problems/maximum-length-of-subarray-with-positive-product/
class Solution {
public:
    int getMaxLen(vector<int>& nums) {
        // 1. 创建 dp 表
        // 2. 初始化
        // 3. 填表
        // 4. 返回结果
        int n = nums.size();
        vector<int> f(n + 1), g(n + 1);
        int ret = INT_MIN;
        for (int i = 1; i <= n; i++) {
            if (nums[i - 1] > 0) {
                f[i] = f[i - 1] + 1;
                g[i] = g[i - 1] == 0 ? 0 : g[i - 1] + 1;
            } else if (nums[i - 1] < 0) {
                f[i] = g[i - 1] == 0 ? 0 : g[i - 1] + 1;
                g[i] = f[i - 1] + 1;
            }
            ret = max(ret, f[i]);
        }
        return ret;
    }
};
